package com.work.lxykeeper.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.work.lxykeeper.R;
import com.work.lxykeeper.bean.HomeFourBean;

import java.util.List;


/**
 * Created by Aly on 2016/9/12 0012.
 * 首页常用GridviewAdapter
 */
public class HomeFourGridViewAdapter extends BaseAdapter {
    private Context context;
    private List<HomeFourBean> datas;
    public HomeFourGridViewAdapter(Context context, List<HomeFourBean> datas){
        this.context=context;
        this.datas=datas;
    }
    //返回子项的个数
    @Override
    public int getCount() {
        return datas.size();
    }
    //返回子项对应的对象
    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }
    //返回子项的下标
    @Override
    public long getItemId(int position) {
        return position;
    }
    //返回子项视图
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HomeFourBean bean= (HomeFourBean) getItem(position);
        View view;
        ViewHolder viewHolder;
        if(convertView==null){
            view = LayoutInflater.from(context).inflate(R.layout.fragment_four_gridview_item,null);
            viewHolder=new ViewHolder();
            viewHolder.home_four_img=(ImageView)view.findViewById(R.id.home_four_img);
            viewHolder.home_four_title=(TextView)view.findViewById(R.id.home_four_title);
            view.setTag(viewHolder);
        }else{
            view=convertView;
            viewHolder= (ViewHolder) view.getTag();
        }
        viewHolder.home_four_title.setText(bean.getTitle());
        viewHolder.home_four_img.setImageResource(bean.getImgId());
        return view;
    }
    //创建ViewHolder类
    class ViewHolder{
        ImageView home_four_img;
        TextView home_four_title;
    }
}
